#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<int> dp(200003, -0x3f3f3f3f);
    const int sum = 200000;
    const int zero = 100000;
    int N;
    cin >> N;
    vector<int> t(N), f(N);
    dp[100000]=0;
    for (int i = 0; i < N; i++)
        cin >> t[i] >> f[i];

    for (int i = 0; i < N; i++)
        if (f[i] >= 0)
        {
            for (int v = sum; v >= f[i]; v--)
                if(dp[v - f[i]] != -0x3f3f3f3f)
                    dp[v] = max(dp[v], dp[v - f[i]] + t[i]);
        }
        else
        {
            for (int v = 0; v <= sum + f[i]; v++)
                if(dp[v - f[i]] != -0x3f3f3f3f)
                    dp[v] = max(dp[v], dp[v - f[i]] + t[i]);
        }
    int ans = 0;
    for (int i = sum; i >= zero; i--)
        if (dp[i] >= 0)
            ans = max(ans, dp[i] + i - zero); 
    cout << ans << endl;
    return 0;
}